package com.scy.boot04test04.controller;

import com.scy.boot04test04.entity.Friend;
import com.scy.boot04test04.mapper.FriendMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class FriendController {
    @Autowired
    FriendMapper friendMapper;

    @RequestMapping("insertFriend")
    public String insert(Friend friend){
        friendMapper.insert(friend);
        return "添加成功<a href='/'>返回首页</a>";
    }
    @RequestMapping("selectFriend")
    public String select(){
        List<Friend> list = friendMapper.select();
        //把list集合中的数据装金表格标签
        String html = "<table border='1'>";
        html+="<caption>朋友列表列表</caption>";
        html+="<tr><th>id</th><th>朋友姓名</th><th>性别</th><th>关系</th><th>操作</th></tr>";
        for (Friend friend :
                list) {
            html+="<tr>";
            html+="<td>"+friend.getId()+"</td>";
            html+="<td>"+friend.getName()+"</td>";
            html+="<td>"+friend.getGender()+"</td>";
            html+="<td>"+friend.getRel()+"</td>";
            //http://localhost:8080/delete?id=xxx
            html+="<td><a href='/deleteFriend?id="+friend.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html+"<br/>"+"<a href='/'>返回首页</a>";
    }
    @RequestMapping("deleteFriend")
    public String delete(int id){
        friendMapper.delete(id);
        return "删除成功<a href='/'>返回首页</a>";
    }
    @RequestMapping("updateFriend")
    public String update(Friend friend){
        friendMapper.update(friend);
        return "更新成功<a href='/'>返回首页</a>";
    }
}
